/* 2017/11/23 废弃
//绑定字典内容到指定的Select控件
function BindSelect(ctrlName, url) {
	var control = $('#' + ctrlName);
	//设置Select2的处理
	control.select2({
		allowClear : true,
		//formatResult: formatResult,
		//formatSelection: formatSelection,
		escapeMarkup : function(m) {
			return m;
		}
	});

	//绑定Ajax的内容
	$.getJSON(url, function(data) {
		var orgIdVal = $("#orgIdVal").val();
		//control.empty();//清空下拉框
		var dataArray = eval('(' + data + ')');
		for (var i = 0; i < dataArray.length; i++) {
			var orgName = dataArray[i].orgName;
			var selected = "";
			if (orgIdVal == dataArray[i].id) {
				selected = "selected";
			}
			control.append("<option " + selected + " value='" + dataArray[i].id + "'>&nbsp;" + orgName + "</option>");
		}
		//绑定默认值
		control.val(orgIdVal).trigger("change");
	});
}*/

//初始化组织下来数选择
var myTree;
var arrDept = new Array(); //项目所属组织集合,用来保存上传集合
function initDeptSelect(){
	var setting = {
			data : {
				key : {
					name : "orgName"
				},
				simpleData : {
					enable : true,
					idKey : "id",
					pIdKey : "parentId"
				}
			},
			check : {
				chkboxType : {"Y": "", "N": ""},//设置勾选既不影响父节点也不影响子节点
				enable : true
			},
			callback : {
				onClick : nodeClick,
				onCheck:nodeCheck
			}
		};
	myTree = $.fn.zTree.init($("#deptTree"), setting,orgDeptCheck);
	//管理隶属增加 获得焦点显示组织列表
	$("#deptSelectView").focus(function () {
		$("#deptTree").show(1000);
    });
	$("input:not(#deptSelectView)").focus(function () {
    	$("#deptTree").hide(1000);
    });
	
	initArrDept();
	
}
//初始化数组中的数据
function initArrDept(){
	var nodes=myTree.getCheckedNodes(true);
	$.each(nodes,function(i,v){
		arrDept.push(v);
	});
	setDeptCorr();
}



$.validator.setDefaults({
	submitHandler : function() {
		saveDocument();
	}
});

$(document).ready(function() {
	var url = "admin/org/dept/json/root";

	$("#lockState").select2({
		minimumResultsForSearch : -1
	});

	var lockStateVal = $("#lockStateVal").val();
	$("#lockState").val(lockStateVal).trigger("change");

	//BindSelect("orgId", url);
	initDeptSelect();
	
	$("#commentForm").validate({
		errorPlacement : function(error, element) {
			// Append error within linked label
			$(element).next().append(error);
		},
		errorElement : "span",
		rules : {
			reading : {
				maxlength : 9,
				max : 999999,
				min : 0
			}
		},
		messages : {
			typeName : {
				required : " 必需字段!",
				minlength : " (不能少于 3 个字母)"
			},
			unitNumber : {
				required : " 必须字段！ "
			}
		}
	});
});

function saveDocument() {
	var id = $("#id").val();
	var orgIds = new Array();
	$.each(arrDept,function(i,v){
		orgIds.push(v.id);
	});
	if (orgIds.length<1) {
		alert("请选择用户组织！");
		return false;
	}
	var userName = $("#userName").val();
	var mobileNumber = $("#mobileNumber").val();
	var userTitle = $("#userTitle").val();
	var lockState = $("#lockState").val();
	var sortNum = $("#sortNum").val();
	var userId=$("#userId").val();
	var url = "admin/org/user/update";

	jQuery.ajax({
		type : "post",
		async : false,
		url : url,
		dataType : "json",
		data : {
			id : id,
			orgIds:JSON.stringify(orgIds),
			userId : userId,
			userName : userName,
			mobileNumber : mobileNumber,
			userTitle : userTitle,
			lockState : lockState,
			sortNum : sortNum
		},
		success : function(data) {
			if (data.success) {
				alert(data.msg);
				location.href = "/pms/admin/org/user/list";
			} else {
				alert(data.msg);
			}
		}
	});
}


//Ztree 节点点击事件,用来控制点击节点名称勾选复选框
function nodeClick(event, treeId, treeNode) {
	//console.log(treeNode);
	if (treeNode.checked) { //当前节点为选中状态
		//取消选中
		myTree.checkNode(treeNode, false, true);
		arrDept.remove(treeNode);
	} else {
		myTree.checkNode(treeNode, true, true);
		arrDept.push(treeNode);
	}
	setDeptCorr();
}
//节点复选框事件,用来控制添加节点数据到数组
function nodeCheck(event, treeId, treeNode){
	if (treeNode.checked) { //当前节点为选中状态
		//取消选中
		arrDept.push(treeNode);
	} else {
		arrDept.remove(treeNode);
	}
	setDeptCorr();
}
//将arrDept数组中的值放到对应input(view,value)中
function setDeptCorr(){
	var view= $("#deptSelectView");
	var s="";
	$.each(arrDept,function(i,v){
		s+=v.orgName;
		if(i!=arrDept.length-1){
			s+=",";
		}
	});
	view.val(s);
}


//为数组创建删除指定元素方法
Array.prototype.indexOf = function(val) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == val) return i;
	}
	return -1;
};
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};


